package com.nit.lab.service.system.impl;

import com.nit.lab.dto.system.PermissionDTO;
import com.nit.lab.dto.system.RoleDTO;
import com.nit.lab.dto.system.TreeDTO;
import com.nit.lab.dto.utils.PagingDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.entity.system.RoleEntity;
import com.nit.lab.mapper.system.RoleMapper;
import com.nit.lab.service.system.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired(required = false)
    private RoleMapper roleMapper;

    @Override
    public TableRspDTO list4Table(TableReqDTO tableReqDTO) throws Exception {
        Long count = roleMapper.count4Table(
                tableReqDTO.parseQueryParam("name"),
                tableReqDTO.parseQueryParam("describe"));
        PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.getPageNo(),
                tableReqDTO.getPageSize(),
                count,
                roleMapper.list4Table(
                        tableReqDTO.getStart(),
                        tableReqDTO.getPageSize(),
                        tableReqDTO.parseQueryParam("name"),
                        tableReqDTO.parseQueryParam("describe"),
                        tableReqDTO.getSortField(),
                        tableReqDTO.getSortOrder()));
        TableRspDTO tableRspDTO = new TableRspDTO(pagingDTO);
        return tableRspDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer add(RoleDTO roleDTO) throws Exception {
        RoleEntity roleEntity = new RoleEntity();
        roleEntity.setRoleName(roleDTO.getName());
        roleEntity.setRoleInfo(roleDTO.getDescribe());
        roleEntity.setGmtCreate(new Date());
        return roleMapper.add(roleEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer update(RoleDTO roleDTO) throws Exception {
        RoleEntity roleEntity = new RoleEntity();
        roleEntity.setRoleId(Long.parseLong(roleDTO.getId()));
        roleEntity.setRoleName(roleDTO.getName());
        roleEntity.setRoleInfo(roleDTO.getDescribe());
        return roleMapper.update(roleEntity);
    }

    @Override
    public Integer updateOrder(Long id, Integer order) throws Exception {
        return roleMapper.updateOrder(id, order);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer remove(String roleIds) throws Exception {
        String[] arrayRoleId = roleIds.split(",");
        for (int i = 0; i < arrayRoleId.length; i++) {
            roleMapper.removeAuthorization(Long.parseLong(arrayRoleId[i]));
            roleMapper.remove(Long.parseLong(arrayRoleId[i]));
        }
        return arrayRoleId.length;
    }

    @Override
    public RoleDTO getRoleByRoleId(Long roleId) throws Exception {
        return roleMapper.getRoleByRoleId(roleId);
    }

    @Override
    public List<RoleDTO> listRole() throws Exception{
        return roleMapper.listRole();
    }

    @Override
    public List<PermissionDTO> listPermissionByRoleId(Long roleId) throws Exception {
        return roleMapper.listPermissionByRoleId(roleId);
    }

    @Override
    public List<TreeDTO> listPermissionByRoleId4Tree(Long roleId) throws Exception {
        List<PermissionDTO> listPermission4Tree = roleMapper.listPermissionByRoleId4Tree(roleId);
        List<TreeDTO> listMenuId4Tree = new ArrayList<TreeDTO>();
        TreeDTO treeDto = new TreeDTO();
        for (int i = 0; i < listPermission4Tree.size(); i++) {
            treeDto = new TreeDTO();
            if (listPermission4Tree.get(i).getActionList().equals("1")) {
                treeDto.setKey(Integer.parseInt(listPermission4Tree.get(i).getRoleId()));
                treeDto.setIsLeaf(1);
                listMenuId4Tree.add(treeDto);
            } else {
                treeDto.setKey(Integer.parseInt(listPermission4Tree.get(i).getRoleId()));
                treeDto.setIsLeaf(2);
                listMenuId4Tree.add(treeDto);
            }
        }
        return listMenuId4Tree;
    }

    @Override
    public List<TreeDTO> listPermission4Tree() throws Exception {
        TreeDTO treeDto;
        List<TreeDTO> listTreeTwo;
        List<TreeDTO> listTreeOne = new ArrayList<TreeDTO>();
        List<PermissionDTO> listPermissionDto = roleMapper.listPermission();
        // 循环中字段字段进行了替换，参看mapper层
        for (int i = 0; i < listPermissionDto.size(); i++) {
            if (listPermissionDto.get(i).getActionList().equals("1")) {
                listTreeTwo = new ArrayList<TreeDTO>();
                for (int j = 0; j < listPermissionDto.size(); j++) {
                    if (listPermissionDto.get(j).getActions().equals(listPermissionDto.get(i).getRoleId())) {
                        treeDto = new TreeDTO();
                        treeDto.setTitle(listPermissionDto.get(j).getPermissionName());
                        treeDto.setKey(Integer.parseInt(listPermissionDto.get(j).getRoleId()));
                        listTreeTwo.add(treeDto);
                    }
                }
                treeDto = new TreeDTO();
                treeDto.setTitle(listPermissionDto.get(i).getPermissionName());
                treeDto.setKey(Integer.parseInt(listPermissionDto.get(i).getRoleId()));
                treeDto.setChildren(listTreeTwo);
                listTreeOne.add(treeDto);
            }
        }
        return listTreeOne;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer authorize(Long roleId, String menuIds) throws Exception {
        roleMapper.removeAuthorization(roleId);
        String[] arrayMenuId = menuIds.split(",");
        for (int i = 0; i < arrayMenuId.length; i++) {
            if (!arrayMenuId[i].equals("")) {
                roleMapper.addAuthorization(roleId, Long.parseLong(arrayMenuId[i]), new Date());
            }
        }
        return arrayMenuId.length;
    }
}
